{% extends "cmdb/index.html" %}
{% block title %}用户操作日志查询{% endblock %}
{% block style %}
    <link type="text/css" rel="stylesheet" href="/static/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/static/css/jquery-ui-1.7.1.custom.css" />
{% endblock %}
{% block breadcrumb  %}
    <ul class="breadcrumb">
                            <li>
                                <i class="ace-icon fa fa-home home-icon"></i>
                                <a href="#">Home</a>
                            </li>
                            <li>
                                <i class="ace-icon fa fa-home home-icon"></i>
                                <a href="#">日志管理</a>
                            </li>
                            <li class="active">日志查询</li>
                        </ul><!-- /.breadcrumb -->
{% endblock %}
{% block page-header %}
<div class="page-header">
    <h1>
        Dashboard
        <small>
            <i class="ace-icon fa fa-angle-double-right"></i>
        日志管理 &amp; 日志查询
        </small>
        </h1>
</div><!-- /.page-header -->
{% endblock %}

{% block content %}
{% include "game/search_form.html"  %}
   <div id="mytable">
     <table class="table table-striped" id="gridtable" width="100%" border="0.5">
        <thead>
        <tr>
{#            <th><input type="checkbox" name="check" id="all"/> </th>#}
              <th>ID</th>
               <th>用户名</th>
                <th>method</th>
                <th>访问IP</th>
            <th>访问时间</th>
                <th>app名</th>
                <th>url_name</th>
            <th>GET参数</th>
            <th>POST参数</th>
            <th>文件参数</th>
        </tr>
        </thead>
        {% for i in posts  %}
            <tr>
{#            <td><input   type="checkbox" name="check_box" value="{{ i.id }}" /> </td>#}
            <td class="active" >{{ i.id }}</td>
            <td class="success" >{{ i.user }}</td>
            <td class="warning">{{ i.method }}</td>

                <td class="success" >{{ i.REMOTE_ADDR }} </td>
            <td>{{ i.start_datetime|date:"Y-m-d H:i:s"  }}</td>
            <td class="warning">{{ i.namespace }}</td>
            <td>{{ i.url_name }}</td>
            <td>{{ i.get_args }}</td>
            <td>{{ i.post_args }}</td>
            <td>{{ i.file_args }}</td>
            </tr>
        {% endfor %}
    </table>
   </div>
    
     <div class="text-center">
    <label for="select">每页数据条数:</label>
    <select  id="mySelect">
        <option>0</option>
        <option>10</option>
        <option>30</option>
         <option>50</option>
         <option>100</option>
        <option>200</option>
        <option>500</option>
        <option>1000</option>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;
    
    
    
    <div class="text-right">
    <ul class="pagination pagination-lg">
        <li class="disabled"><a href="#">&laquo;</a></li>
        {% for var in pagelist %}
            {% ifequal var curpage  %}
            <li class="active"><a href="{% url url_name  %}?curpage={{var}}&allpage={{allpage}}"> {{var}}</a></li>

            {% else %}
                <li><a href="{% url url_name  %}?curpage={{var}}&allpage={{allpage}}">{{var}}</a></li>
                {% endifequal %}
        {% endfor %}
    <li ><a href="#">&raquo;</a></li>
    </ul>
    </div>
    </div>


    <!-- jqGrid 分页 div gridPager -->
{#<div id="gridPager">#}
{#    <input  class="btn btn-default" type="button" id="add" value="添加" />#}
{#    <input class="btn btn-warning" type="button" id="edit" value="编辑" />#}
{#    <input class="btn btn-danger" type="button" id="delete" value="删除" />#}
{##}
{#</div>#}
    <!--jqGrid 浏览导航栏所在-->
<p style="display:none">默认隐藏</p>
<!-- 模态框（Modal） -->
<div class="modal fade" id="mymodal"  >
    <div class="modal-dialog" style="width:520px;">
        <div class="modal-content">
            <p><button class="close" type="button" data-dismiss="modal">×</button></p>
                <hr />
                <p id="label_for_edit"  name="label_modal" class="text-center">aaaa</p>
                <!-- 模态弹出窗内容 -->
         <!-- <div class="modal-body">
            <textarea rows="2" cols="50">在这里输入回复</textarea>
          
         </div> -->
         <form id="myformid"  class="form-horizontal" method="POST" role="form"  onSubmit="return false;">
           {% csrf_token %}
          <input type="hidden" name="id"  value="">
        {% for field in form %}        
          <div class="form-group"></div>
          <label for={{ field.label }} class="col-sm-3 control-label">{{ field.label }}</label>
          <div class="col-sm-8">{{ field }} </div>
         {{ field.errors }}
     {% endfor %}
        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">

                            <button type="submit" class="btn btn-success" onclick="postform()">确定</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <button  id="btn2" type="button" class="btn btn-primary"   data-dismiss="modal">关闭</button>
                        </div>
                        </div>
        </form>  
         </div>
      </div><!-- /.modal-content -->
</div><!-- /.modal -->

{% endblock %}

    {% block scripts %}
        <script  type="text/javascript" src="/static/js/bootstrap.min.js"></script>
       
    <script type="text/javascript">
    function getCookie(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

    if(arr=document.cookie.match(reg))

        return unescape(arr[2]);
    else
        return null;
}

function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

    function postform(){
       var baseurl="{% url url_name  %}?&r="+Math.random();
       option=getCookie('option');
       var url = baseurl+"&option="+option;
       delCookie('option');
        data=$('#myformid').serialize();
        var parent_id=$("#id_parent_id").val();
       if (parent_id==""||parent_id==undefined || parent_id==null){

           parent_id=0
       }
        var prent_id=parseInt(parent_id);
        var text=$('input[id="id_text"]').val();
        var url_name=$('input[id="id_url_name"]').val();
        var addition=$('input[id="id_addition"]').val();
        var namespace=$('input[id="id_namespace"]').val();
        var id=$('input[name="id"]').val();
{#        alert(namespace);#}
        data={text:text,url_name:url_name,parent_id:parseInt(prent_id),id:id,namespace:namespace,csrfmiddlewaretoken:"{{csrf_token}}"};
{#        alert(data);#}
       $.ajax({
                cache: false,
                type: "POST",
                url:url,
                //data:JSON.stringify(data),//$('#myformid').serialize(),// 你的formid
                data:data,
              // contentType: "application/json; charset=utf-8",
                async: false,
                dataType: "json",
                error: function(request) {
                    alert("Connection error");
                },
                success: function(data) {
                    alert(data.msg);
                    delCookie('pageper');

                    $("#mymodal").modal('hide');
                    var url="{% url url_name  %}?&r="+Math.random();
                    window.location.href=url;
                 }
            }); 
    }

$(document).ready(function() {
    //window.onload=onload;


    $('#mySelect').change(function(){
        var per=$(this).children('option:selected').val();
        delCookie('pageper');
        setCookie('pageper',per);
        var url="{% url url_name   %}?&r="+Math.random();
        window.location.href=url;
        });


    $("#add").click(function () {
        $('p[name="label_modal"]').html("添加菜单");
        $('input').attr("value","");
        $('input[name="csrfmiddlewaretoken"]').attr("value","{{csrf_token}}");
        $("#mymodal").modal('show');
        setCookie('option','add');
    });


  function onload(){
    
      var modal =getCookie("modal");
      var serverid=getCookie("server_id");
      if (modal =="yes"){
        $('p[name="label_modal"]').html("编辑菜单");
        $('input[name="id"]').attr("value",serverid);
        $("#mymodal").modal('show');
        delCookie("modal");
        delCookie("server_id");

      }
      
  }


    //编辑对应的js操作
    $("#edit").click(function () {
        var len = $("input:checkbox:checked").length;
        if (len>1){
            alert("一次只能编辑一个");
            return false;
        }
        else if  (len==0){
            alert("你还没有选中");
            return false;
        }
        var chk_value =[];
        $('input[name="check_box"]:checked').each(function(){
        chk_value.push($(this).val());
        });
        var server_id=chk_value[0];
        $('input').attr("value",""); //删除form表单的信息
        $('input[name="csrfmiddlewaretoken"]').attr("value","{{csrf_token}}"); //
        var url= "{% url url_name  %}?option=query&r="+Math.random();
        $.ajax(
                {
                    type: "POST",
                    url:url,
                    data: {serverid:server_id,csrfmiddlewaretoken:"{{csrf_token}}",option:"query"},
                    dataType: "json",
                    success: function(ret){
                    },
                    error: function (msg) {
                        alert("Connection error");
                        return  false;
                    }
                }

        );
        setCookie('option','update');
        $("#mymodal").modal('show');

    });



    $("#delete").click(function () {
        var chk_value =[];
        $('input[name="check_box"]:checked').each(function(){
        chk_value.push($(this).val());
        });
        var server_id=chk_value.join('.');
        var len = $("input:checkbox:checked").length;
        if  (len<1){
            alert("你还没有选中");
            return false;
        }
        var result=confirm("是否删除id为:["+server_id+"]主机");
        if (! result){
            return false;
        }
        var url= "{% url url_name  %}?option=del&r="+Math.random();
         $.ajax(
                {
                    type: "POST",
                    url:url,
                    data: {serverid:server_id,csrfmiddlewaretoken:"{{csrf_token}}",option:"delete"},
                    dataType: "json",
                     success: function(ret){
                        delCookie('pageper');
                          var url="{% url url_name  %}?&r="+Math.random();
                          window.location.href=url;
                     },
                     error: function (msg) {
                          delCookie('pageper');
                          var url="{% url url_name  %}?&r="+Math.random();
                          window.location.href=url;
                    }
                }
                  )
    });



    $("#all").click(function(){
        if(this.checked){
            $("[name=check_box]:checkbox").prop("checked", true);
        }else{
            $("[name=check_box]:checkbox").removeAttr("checked");
        }
     });

    $("#search_btn").click(function(){

    });
    function get_url(url) {
        alert(url);
        return false;



    }
});
    </script>
        {% endblock %}
